package com.zhu.controller;

import com.zhu.pojo.Business;
import com.zhu.pojo.CustBus;
import com.zhu.pojo.Customer;
import com.zhu.pojo.Result;
import com.zhu.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.geom.RectangularShape;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @PostMapping("/searchname")
    public Result searchNames(){
        try {
            List<Map<String, Object>> list = businessService.SearchNames();
            if (list == null) {
                return Result.fail("没有数据呢");
            }
            return Result.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("获取数据时发生错误");
        }
    }

    @PostMapping("getbusid")
    public int getbusid(@RequestBody String name){
        return businessService.getbusid(name);
    }

    @PostMapping("getcustid")
    public int getcustid(@RequestBody String name){return businessService.getcustid(name);}

    @PostMapping("/custaddbus")
    public Result custaddbus(@RequestBody CustBus custBus){
        custBus.setCreate_time(new Date());
        try{
            businessService.custlinkbus(custBus);
            return Result.ok();
        }catch (Exception e){
            System.out.println(custBus);
            e.printStackTrace();
            return Result.fail("发生错误");
        }
    }

    @PostMapping("/getallbusid")
    public List<Integer> getallbusid(@RequestBody Integer id){
        return businessService.getallbusid(id);
    }

    @PostMapping("/getallbus")
    public List<Business> getallbus(@RequestBody List<Integer> ids){
        return businessService.findBusinessesByNames(ids);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Business business){
        try {
            businessService.update(business);
            return Result.ok();
        }catch (Exception e){
            return Result.fail("发生错误");
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Business business){
        try{
            businessService.delete(business);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("发生错误");
        }
    }

    @PostMapping("/search")
    public Result search(@RequestBody Business business){
        try{
            List<Map<String,Object>> list = businessService.search(business);
            return Result.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("发生错误");
        }
    }

    @PostMapping("/searchByBusName")
    public Result searchByBusid(@RequestBody String business_name){
        try{
            List<Customer> list = businessService.searchcust(business_name);
            return Result.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("发生错误");
        }
    }

    @PostMapping("/getlist")
    public List<Map<String,Object>> getlist(){
        return businessService.getstaticlist();
    }
}
